package cn.blunce.app.base;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import cn.blunce.app.models.DaoMaster;
import cn.blunce.app.models.DaoMaster.DevOpenHelper;
import cn.blunce.app.models.DaoSession;
import cn.blunce.app.models.JobDao;
import cn.blunce.app.models.Person;
import cn.blunce.app.models.PersonDao;
import cn.blunce.app.models.PersonDao.Properties;

public class PersonDB {
	private static DevOpenHelper helper;
	private static SQLiteDatabase mDatabase;
	private static DaoMaster mDaoMaster;
	private static DaoSession mDaoSession;
	private static PersonDao mPersonDao;
	private static JobDao mJobDao;
	private static Context mContext;

	private static PersonDB sPersonDB = null;

	private PersonDB(Context context) {
		// TODO Auto-generated constructor stub
		mContext = context;
		helper = new DaoMaster.DevOpenHelper(context, "person-db", null);
		mDatabase = helper.getWritableDatabase();
		mDaoMaster = new DaoMaster(mDatabase);
		mDaoSession = mDaoMaster.newSession();
		mPersonDao = mDaoSession.getPersonDao();
		mJobDao = mDaoSession.getJobDao();
	}

	public static synchronized PersonDB getInstance(Context context) {
		if (sPersonDB == null) {
			sPersonDB = new PersonDB(context);
		}
		return sPersonDB;
	}

	public static void addPerson(Person person) {
		mPersonDao.insert(person);
	}

	public static void deletePerson(Person person) {
		mPersonDao.delete(person);
	}

	public static void modifyPerson(Person person) {
		mPersonDao.insertOrReplace(person);
	}

	public static List<Person> findPersonByName(String name) {
		return mPersonDao.queryBuilder().where(Properties.Name.eq(name)).list();
	}

	// KEEP METHODS - put your custom methods here

	public static boolean add(Object object) throws NoSuchFieldException,
			ClassNotFoundException, InstantiationException,
			IllegalAccessException, NoSuchMethodException,
			IllegalArgumentException, InvocationTargetException {
		boolean tag = true;
		PersonDB tempDB = new PersonDB(mContext);
		String dbName = "m"
				+ object.getClass()
						.getName()
						.substring(
								object.getClass().getName().lastIndexOf(".") + 1)
				+ "Dao";
		Field field = tempDB.getClass().getDeclaredField(dbName);
		field.setAccessible(true);

		// Class clazz = (Class) Class
		// .forName(object.getClass().getName() + "Dao");
		// Object o = tempDB.getClass().getDeclaredField(dbName);
		Object o = field.get(tempDB);
		// o.i
		Method x = o.getClass().getSuperclass()
				.getMethod("insert", new Class[] { object.getClass() });
		x.invoke(o, object);
		return tag;
	}

	/*
	 * String dbName = "m" + object.getClass() .getName() .substring(
	 * object.getClass().getName().lastIndexOf(".") + 1) + "Dao"; Field field =
	 * tempDB.getClass().getDeclaredField(dbName); field.setAccessible(true);
	 * Class clazz = (Class) Class .forName(object.getClass().getName() +
	 * "Dao"); // Object o = clazz.newInstance(); // // o =
	 * object.getClass().getDeclaredField(m + dbName); // o = field.get(tempDB);
	 */

	public static DaoMaster getmDaoMaster() {
		return mDaoMaster;
	}

	public static DaoSession getmDaoSession() {
		return mDaoSession;
	}

	public static PersonDao getmPersonDao() {
		return mPersonDao;
	}

	public static JobDao getmJobDao() {
		return mJobDao;
	}

}
